import docx
import re
import jieba
from pymongo import MongoClient

client = MongoClient('localhost',27017)
db = client.py06db
#
# file = docx.Document('words.docx')
# str2 = ''
# for i in file.paragraphs:
#     str1 = i.text
#     str1 = str1.strip()
#     # print(str1)
#
#     if str1.isdigit() == False:
#         han = re.compile(u'[\u4e00-\u9fa5]')
#         match = han.search(str1)
#         # print(match)
#         if match and '    ' not in str1:
#             if '答案' not in str1:
#                 str2+=str1
#                 # print(str2)
#             else:
#                 # print(str1)
#
#                 use_1 = {
#                     "issue":str2,
#                     "result":str1,
#                 }
#
#                 db.tiku.insert_one(use_1)
#
#                 str2 = ''


##db.tiku.create_index([('issue',1)]) # 创建索引
documents = db.tiku.find()

sou = input('请输入关键词：')
list1 = []
for document in documents:
    # print(document['issue'])
    cut = jieba.cut(document['issue'])
    sss = (','.join(jieba.cut_for_search(document['issue'])))
    ssss = (','.join(cut))
    if sou in sss or sou==document['issue']:
        if document['issue'] not in list1:
            list1.append(document['issue'])
            print(document['issue'],re.findall(r'答案.*?.$',document['result'])[0])

